package table;

import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.layout.ColumnLayout;
import org.uqbar.arena.layout.HorizontalLayout;
import org.uqbar.arena.layout.VerticalLayout;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Container;
import org.uqbar.arena.widgets.Label;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.tables.Column;
import org.uqbar.arena.windows.SimpleWindow;
import org.uqbar.arena.windows.WindowOwner;
import windows.bet.ChooseBet;
import domain.Bet;
import domain.Player;
import domain.Table;
import domain.extra.FullTableException;

public class RouletteWindow extends SimpleWindow<Table>{
	private Player actualPlayer ; 
	
	public RouletteWindow(WindowOwner parent, Player model) throws FullTableException {
		super(parent, new Table());
		System.out.println("Modelo: " + getModel());
		System.out.println("Jugador: "+ model);
		getModel().addPlayer(new Player("nati", 154));
		actualPlayer = model; 
		getModel().addPlayer(model);
	}

	@Override
	protected void createFormPanel(Panel mainPanel) {
		Panel form = new Panel(mainPanel);
		form.setLayout(new VerticalLayout());
		
		Label labelPlayer = new Label(form);
		String hello = "Hello " + actualPlayer.getName()+"!";
		labelPlayer.setText(hello);
		
		Label labelChips = new Label(form);
		String countChips = "Chips: " + actualPlayer.getChips();
		labelChips.setText(countChips);
		
		Label labelMoney = new Label(form);
		String countMoney = "Money: " + actualPlayer.getMoney();
		labelMoney.setText(countMoney);
		
		Panel column = new Panel(form);
		column.setLayout(new ColumnLayout(2));
		
		//describeResultsGridBet(column);
		describeResultsGridPlayer(column); 
		
		Panel buttons = new Panel(form);
		buttons.setLayout(new HorizontalLayout());
		
		buttonsPanel(buttons); 
	}
	
	public void buttonsPanel (Panel panel) {
		Button changeChipsButton = new Button(panel);
		changeChipsButton.setCaption("changeChips");
		changeChipsButton.onClick(new MessageSend(this, "changeChips"));
		
		Button rouletteSpinButton = new Button(panel);
		rouletteSpinButton.setCaption("rouletteSpin");
		rouletteSpinButton.onClick(new MessageSend(this, "rouletteSpin"));
		
		Button withdrawButton = new Button(panel); 
		withdrawButton.setCaption("withdraw");
		withdrawButton.onClick(new MessageSend(this, "withdraw"));
		
		Button toBetButton = new Button(panel);
		toBetButton.setCaption("toBet"); 
		toBetButton.onClick(new MessageSend(this, "toBet"));
	}
	
	public void withdraw (){
		getModel().removePlayer(actualPlayer);
		this.close(); 
		System.out.println("Actual player: "+getModel().getPlayers());
	}
	
	public void toBet(){
		new ChooseBet(getOwner()).open();
	}
	
	public void changeChips (){
		new ChangeChipsWindow(this, actualPlayer).open();
	}
	
	public void rouletteSpin (){
		new RouletteSpinWindow(this, getModel()).open(); 
	}
	
	protected void describeResultsGridPlayer(Panel mainPanel) {
		Container container = mainPanel;
		org.uqbar.arena.widgets.tables.Table<Player> table = new org.uqbar.arena.widgets.tables.Table<Player>(container , Player.class);
		
		Column<Player> nombreColumn = new Column<Player>(table);
		nombreColumn.setTitle("Nombre");
		nombreColumn.setFixedSize(100);
		nombreColumn.bindContentsToProperty(Player.NAME);

		table.bindContentsToProperty(domain.Table.PLAYERS);
	}
	
//	protected void describeResultsGridBet(Panel mainPanel) {
//		Container container = mainPanel;
//		org.uqbar.arena.widgets.tables.Table<Bet> table = new org.uqbar.arena.widgets.tables.Table<Bet>(container , Bet.class);
//		
//		Column<Bet> nombreColumn = new Column<Bet>(table);
//		nombreColumn.setTitle("Nombre");
//		nombreColumn.setFixedSize(100);
//		nombreColumn.bindContentsToProperty(Player.NAME);
//
//		table.bindContentsToProperty(domain.Table.PLAYERS);
//	}
	
	@Override
	protected void addActions(Panel actionsPanel) {
		
	}
	
	protected void createMainTemplate(Panel formBuilder) {
		this.setTitle(" The Happy Roulette " );
		super.createMainTemplate(formBuilder);
	}
}
